package com.javasm.file;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import com.javasm.commons.entity.AxiosResult;
import com.javasm.commons.entity.CrmProperties;
import com.javasm.commons.enums.E;
import com.javasm.commons.exceptions.MvcException;
import com.javasm.commons.util.OssUtil;
import com.javasm.oss.IOssService;
import com.javasm.oss.OssFiles;
import com.javasm.sys.CurrentLogin;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("oss")
public class OssController {
    @Resource
    private IOssService ossService;

    @GetMapping("download")
    public ResponseEntity download(String relName, String bucketName, String objectName) throws IOException {
        InputStream inputStream = OssUtil.download(bucketName, objectName);
        byte[] bytes = IoUtil.readBytes(inputStream);

        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attachment;filename=" + URLEncoder.encode(relName,"UTF-8"));

        return new ResponseEntity(bytes,headers, HttpStatus.OK);
    }
    @DeleteMapping()
    public AxiosResult del(String id, String bucketName, String objectName){
        OssUtil.del(bucketName,objectName);
        boolean b = ossService.removeById(id);
        return AxiosResult.suc();
    }

    @GetMapping("listbyIds")
    public AxiosResult listOssFileByIds(String ids){
        String[] idsArray = ids.split(",");
        List<OssFiles> ossFiles = ossService.listByIds(Arrays.asList(idsArray.clone()));
        return AxiosResult.suc(ossFiles);
    }

    @PostMapping("uploadFile")
    public AxiosResult doUploadFile(MultipartFile[] myfile, String dir) throws IOException {
        if(StrUtil.isEmpty(dir))throw new MvcException(E.DIR_NULL);
        String loginUserName = CurrentLogin.getLoginUserName();
        List<OssFiles> fs = new ArrayList<>();
        for (MultipartFile multipartFile : myfile) {
            InputStream inputStream = multipartFile.getInputStream();
            String originalFilename = multipartFile.getOriginalFilename();
            String prefix = originalFilename.substring(originalFilename.indexOf("\\.") + 1);
            long size = multipartFile.getSize();

            String objectName=dir+"/"+ UUID.randomUUID().toString()+prefix;
            OssUtil.uploadFile(CrmProperties.getOss().getStandBuckName(),objectName,inputStream);

            OssFiles f = new OssFiles();
            f.setBucketName(CrmProperties.getOss().getStandBuckName());
            f.setObjectName(objectName);
            f.setFileSize((double)size);
            f.setRealName(originalFilename);
            f.setUploadTime(DateUtil.now());
            f.setUploadUser(loginUserName);
            fs.add(f);
        }

        ossService.saveBatch(fs);

        Map<String,String> map = new HashMap<>();
        map.put("ossIds", fs.stream().map(OssFiles::getId).collect(Collectors.joining(",")));
        return AxiosResult.suc(map);
    }
}
